<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Http\Controllers\Controller;
use App\Pro; #引入model
use App\Att;
use DB;
use App\Bid;

class ProController extends Controller
{

	
    public function jie(){
    	return view('auth.woyaojiekuan');
    }
    public function jiePost(Request $rep){
    	$pro = new Pro();
    	$att = new Att();
    	
      
    	#接受POST过来的数据 主表 把收到的金额转成分
    	$pro->money = intval($rep->money) * 100;
    	$pro->mobile= $rep->mobile;
    	$pro->pubtime=time();
        $pro->name=$rep->user()->name; 
    	$pro->uid=$rep->user()->uid;      
    	$pro->save();
    	
    	// #写附表
    	$att->pid = $pro->pid;
    	$att->uid = $rep->user()->uid;

    	$att->save();
    	return redirect('/prolist');
    }


    public function pro($pid){
        $pro = Pro::find($pid);
        return view('auth.lijitouzi',['pro'=>$pro]);
    }
    public function touzi(Request $req,$pid){
       
        $bid = new Bid();
      
        $bid->uid = $req->user()->uid;
        $bid->pid = $pid;
        $bid->title = $req->title;
        $bid->money = $req->money*100 ;
        $bid->pubtime = time();
         $bid->save();

        $pro=Pro::find($pid);
        $pro->recive += $bid->money;
        $pro->save();
       
       if($pro->recive == $pro->money){
        $this->touziDone();
       }
        return '购买成功';
     
    }
    protected function touziDone($pid){
        #修改status的值到还款中/收益 
        $pro = Pro::find($pid);
        $pro->status =2;
        $pro->save();


        #还款
   
       $amount= ($pro->money*$pro->rate/12/100+ $pro->money/$pro->hrange);
       $today =date('Y-m-d');
       for ($i=0; $i < $pro->hrange ; $i++) { 
        $paydate =  date('Y-m-d',strtotime("+ $i months"));
        $row=[
        'uid'=>$pro->uid,'pid'=>$pro->pid,'title'->$pro->title,    
        ] ;
        $row["amount"]=$amount; $row
       }
       DB::table('hks')->create()
    }
}
